package com.example.lbstest.controller;

import com.example.lbstest.impl.StudentMapperServiceImpl;
import com.example.lbstest.impl.UserMapperServiceImpl;
import com.example.lbstest.pojo.Student;
import com.example.lbstest.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentMapperServiceImpl service;

    @RequestMapping("/changePassword")
    @ResponseBody
    public String changePassword(@RequestParam("number")String number,@RequestParam("password")String password){
        int rr = service.changePassword(number,password);
        if(rr == 1){
            System.out.println("----修改密码成功-----");
            return "success";
        }
        return "fail";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest request, HttpServletResponse response,
                         HttpSession session, Model model) {

        String number = request.getParameter("number");
        String password = request.getParameter("password");
        Student student = service.login(number, password);
        System.out.println(student);
        if (student != null) {
            session.setAttribute("student", student);
            System.out.println("----登录成功-----");
            return student.getName();
        }
        return "fail";
    }

    @RequestMapping("/selectOneStudent")
    @ResponseBody
    public Student selectOneStudent(@RequestParam("student_id")String student_id){
        Student student = service.selectOneStudent(student_id);
        if(student != null){
            System.out.println("------查询学生信息成功");
        }
        return student;
    }

    @RequestMapping("/selectPreStudent")
    @ResponseBody
    public List<Student> selectPreUser() {

        List<Student> students = service.selectPreStudent();
        return students;
    }

    @RequestMapping("/selectStudentList")
    @ResponseBody
    public List<Student> selectStudentList(@RequestParam("id")String id) {
        List<Student> students = service.selectStudentList(id);
        System.out.println("---选课学生列表查询成功---");
        return students;
    }
}
